﻿
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>部门管理</title>
    <link href="~/Content/LayerUI/src/css/layui.css" rel="stylesheet" />
    <link href="~/Content/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
    <!-- Main -->
    <div class="layui-tab layui-tab-brief main-tab-container ">
        <div class="layui-tab-content">
            <button class="layui-btn layui-btn layui-btn-small demo" data-type="新增部门"><i class="layui-icon">&#xe654;</i> 新 增 </button>
            <button class="layui-btn layui-btn-warm layui-btn-small demo" data-type="编辑部门"><i class="layui-icon">&#xe642;</i> 编 辑 </button>
            <button class="layui-btn layui-btn-danger layui-btn-small" lay-submit lay-filter="delete"><i class="layui-icon">&#xe640;</i> 删 除 </button>
        </div>
        <div class="layui-tab-content ">
            <form class="layui-form">
                <ul id="departmentTree"></ul>
            </form>
        </div>
    </div>
    <!-- 模态框（新增和编辑） -->
    <div id="add_edit" style="display: none;padding: 20px;">
        <form class="layui-form">
            <div class="layui-form-item" hidden>
                <label class="layui-form-label">部门Id：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_Id">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门名称：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_Name" lay-verify="required" autocomplete="off" placeholder="请输入部门名称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上级部门：</label>
                <div class="layui-input-block">
                    <select name="temp" class="form-control" id="Dt_ParentId" lay-verify="required">
                        <option value="0">无</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">部门类别：</label>
                <div class="layui-input-block">
                    <select name="temp" class="form-control" id="Dt_Level" lay-verify="required">
                        <option value="1" selected>税务局</option>
                        <option value="2">税务所</option>
                        <option value="3">街道办</option>
                        <option value="4">社区</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">编码：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_AmdCode" lay-verify="required" autocomplete="off" placeholder="请输入部门编码" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">全称：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_AmdFullName" lay-verify="required" autocomplete="off" placeholder="请输入部门全称" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">经度：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_BD_Longitude" autocomplete="off" placeholder="请输入部门经度" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">纬度：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_BD_Latitude" autocomplete="off" placeholder="请输入部门纬度" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">排序：</label>
                <div class="layui-input-block">
                    <input type="text" id="Dt_Sortnum" autocomplete="off" placeholder="请输入部门排序" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item" style="text-align: right;">
                <div class="layui-btn layui-btn-normal" lay-submit lay-filter="btnSure">提 交</div>
            </div>
        </form>
    </div>
    <!-- 框架必要 -->
    <script src="~/Content/Zor/Scripts/zor_base.js"></script>
    <script src="~/Content/LayerUI/src/layui.js"></script>
    <script type="text/javascript">
        var dtName = "";
        var dt_Object;        //选中树对象
        var isAddAlter = true;//新增还是修改
        var selectId = 0;     //选中树ID
        /**
         * 请求Ajax
         * 返回Json类型字符串
         * JSON.stringify({ 'foo': 'foovalue', 'bar': 'barvalue' }),  //相当于 //data: "{'str1':'foovalue', 'str2':'barvalue'}",
         */
        function JqPostSimpleData(url, postData, callBack) {
            layui.jquery.ajax({
                type: 'POST',
                url: url,
                contentType: 'application/json',
                dataType: 'json',
                data: postData,
                success: function (data) {
                    callBack(data);
                }
            });
        }

        /**
         * json格式转树状结构
         * param   {json}      json数据
         * param   {String}    id的字符串
         * param   {String}    父id的字符串
         * param   {String}    children的字符串
         * return  {Array}     数组
         */
        function transData(a, idStr, pidStr, chindrenStr) {
            var r = [], hash = {}, Dt_Id = idStr, Dt_ParentId = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
            for (; i < len; i++) {
                hash[a[i][Dt_Id]] = a[i];
            }
            for (; j < len; j++) {
                var aVal = a[j], hashVP = hash[aVal[Dt_ParentId]];
                if (hashVP) {
                    !hashVP[children] && (hashVP[children] = []);
                    hashVP[children].push(aVal);
                } else {
                    r.push(aVal);
                }
            }
            return r;
        }

        /**
         *layui
         */
        layui.use(['element', 'layer', 'form', 'tree'], function () {
            var element = layui.element(), jq = layui.jquery, form = layui.form();
            /**
             *显示列表
             */
            JqPostSimpleData('/Systems/LoadDepartment', '', function (data) {
                var jsonDataTree = transData(data, 'Dt_Id', 'Dt_ParentId', 'children');
                /**
                 *树形菜单结构
                 */
                layui.tree({
                    elem: '#departmentTree'    //指定元素
                    , click: function (item) { //点击节点回调
                        selectId = item.Dt_Id;
                        dt_Object = item;
                    }
                    , nodes: jsonDataTree
                });
            })

            /**
             * 监听提交 - 删除
             */
            form.on('submit(delete)', function (data) {
                //判断是否有选项
                if (selectId == 0) {
                    layer.msg('请选择要删除的部门！');
                    return false;
                }
                JqPostSimpleData('/Systems/SelectDepartment', JSON.stringify({ 'id': selectId }), function (data) {
                    if (data.AnswerType == 1) {
                        layer.confirm("确定删除该部门吗？", { icon: 2, btn: ['确定', '取消'] }, function (index, layero) {
                            JqPostSimpleData("/Systems/DeleteDepartment", JSON.stringify({ 'id': selectId }), function (data) {
                                if (data.AnswerType == 1) {
                                    Replace();
                                }
                                layer.msg(data.AnswerMeg);
                            });
                        })
                    } else {
                        layer.msg(data.AnswerMeg);
                    }
                });
                return false;
            });

            /**
             * 显示弹出框 - 增加、修改
             */
            jq('.demo').on('click', function () {
                var type = jq(this).data('type');
                if (type == '编辑部门') {
                    if (selectId == 0) {
                        layer.msg('请选择要编辑的部门！');
                        return;
                    }
                }
                GetAccordionDtparentid();//加载上级部门下拉框
                layer.open({
                    title: type,
                    type: 1,
                    skin: 'layui-layer-rim',
                    area: ['600px', '620px'],
                    content: jq('#add_edit'),
                    success: function () {
                        if (type == '编辑部门') {
                            dtName = dt_Object.Dt_Name;
                            jq('#Dt_Name').val(dtName);
                            jq('#Dt_Sortnum').val(dt_Object.Dt_Sortnum);
                            jq('#Dt_AmdCode').val(dt_Object.Dt_AmdCode);
                            jq('#Dt_AmdFullName').val(dt_Object.Dt_AmdFullName);
                            jq('#Dt_BD_Longitude').val(dt_Object.Dt_BD_Longitude);
                            jq('#Dt_BD_Latitude').val(dt_Object.Dt_BD_Latitude);
                            jq('#Dt_Level').val(dt_Object.Dt_Level);
                            jq('#Dt_Id').val(dt_Object.Dt_Id);
                            isAddAlter = false;
                        }
                        else {
                            jq('#Dt_Name').val('');
                            jq('#Dt_Sortnum').val('');
                            jq('#Dt_AmdCode').val('');
                            jq('#Dt_AmdFullName').val('');
                            jq('#Dt_BD_Longitude').val('');
                            jq('#Dt_BD_Latitude').val('');
                            jq('#Dt_Level').val('');
                            jq('#Dt_Id').val('');
                            dtName = "";
                            isAddAlter = true;
                        }
                        jq('#Dt_Name').focus();
                    }
                });
            })

            /**
             * 监听提交 -(提交)
             */
            form.on('submit(btnSure)', function (data) {
                var jsonData = GetWebControls(add_edit, jq)
                loading = layer.load();  //显示加载...
                JqPostSimpleData("/Systems/AddAlterDepartment", JSON.stringify({ 'Department': jsonData, 'oldName': dtName, 'isAddAlter': isAddAlter }), function (data) {
                    layer.close(loading);//关闭加载...
                    layer.msg(data.AnswerMeg, function () {
                        if (data.AnswerType == 1) {
                            Replace();
                        }
                    });
                });
            })

            /**
             * 查询所有不为4的部门
             */
            function GetAccordionDtparentid() {
                var html = "<option value='0'>无</option>";
                JqPostSimpleData("/Systems/SelectDtparentid", "", function (data) {
                    jq.each(data, function (i) {
                        html += "<option value='" + data[i].Dt_Id + "' >" + data[i].Dt_Name + "</option>";
                    });
                    jq("#Dt_ParentId").empty().append(html);
                    if (!isAddAlter) {
                        jq('#Dt_ParentId').val(dt_Object.Dt_ParentId);
                    }
                    form.render('select');//刷新select选择框渲染
                })
            }
        })

    </script>
</body>
</html>
